package cn.ziyia.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

//public class MainActivity extends AppCompatActivity {
//    private Button insert, query, delete;
//    private final String AUTHORITY = "com.ziyia.databasetest.provider";
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
//        init();
//
//        insert.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Uri uri = Uri.parse("content://" + AUTHORITY + "/Book");
//                ContentValues contentValues = new ContentValues();
//                contentValues.put("name", "年少有为最新加添");
//                contentValues.put("author", "嘿嘿");
//                contentValues.put("pages", 400);
//                contentValues.put("price", 1.5);
//                Uri newUri = getContentResolver().insert(uri, contentValues);
//            }
//        });
//
//
//        query.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Uri uri = Uri.parse("content://" + AUTHORITY + "/book");
//                Cursor query = getContentResolver().query(uri, null, null, null, null);
//
//                if (query != null) {
//                    while (query.moveToNext()) {
//                        String name = query.getString(query.getColumnIndex("name"));
//                        Log.d("query", name);
//                    }
//                }
//
//            }
//        });
//
//        delete.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Uri uri = Uri.parse("content://" + AUTHORITY + "/book");
//                getContentResolver().delete(uri, null, null);
//            }
//        });
//    }
//
//
//    private void init() {
//        insert = findViewById(R.id.insert);
//        query = findViewById(R.id.query);
//        delete = findViewById(R.id.delete);
//    }
//}

public class MainActivity extends AppCompatActivity {
    private String newId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button addData = (Button) findViewById(R.id.add_data);
        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://com.ziyia.databasetest.provider/book");
                        ContentValues values = new ContentValues();
                values.put("name", "A Clash of Kings");
                values.put("author", "George Martin");
                values.put("pages", 1040);
                values.put("price", 22.85);
                Uri newUri = getContentResolver().insert(uri, values);
                newId = newUri.getPathSegments().get(1);
            }
        });
        Button queryData = (Button) findViewById(R.id.query_data);
        queryData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// 查询数据
                Uri uri = Uri.parse("content://com.ziyia.databasetest.provider/book");
                        Cursor cursor = getContentResolver().query(uri, null, null,
                                null, null);
                if (cursor != null) {
                    while (cursor.moveToNext()) {
                        String name = cursor.getString(cursor.
                                getColumnIndex("name"));
                        String author = cursor.getString(cursor.
                                getColumnIndex("author"));
                        int pages = cursor.getInt(cursor.getColumnIndex
                                ("pages"));
                        double price = cursor.getDouble(cursor.
                                getColumnIndex("price"));
                        Log.d("MainActivity", "book name is " + name);
                        Log.d("MainActivity", "book author is " + author);
                        Log.d("MainActivity", "book pages is " + pages);
                        Log.d("MainActivity", "book price is " + price);
                    }
                    cursor.close();
                }
            }
        });
        Button updateData = (Button) findViewById(R.id.update_data);
        updateData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// 更新数据
                Uri uri = Uri.parse("content://com.ziyia.databasetest.provider/book/" + newId);
                        ContentValues values = new ContentValues();
                values.put("name", "A Storm of Swords");
                values.put("pages", 1216);
                values.put("price", 24.05);
                getContentResolver().update(uri, values, null, null);
            }
        });
        Button deleteData = (Button) findViewById(R.id.delete_data);
        deleteData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// 删除数据
                Uri uri = Uri.parse("content://com.ziyia.databasetest.provider/book/" + newId);
                        getContentResolver().delete(uri, null, null);
            }
        });
    }
}

class MyThread{

    public static void main(String[] args) {
        Thread thread = new Thread(() ->{
            // Code...
        });
        thread.start();
    }
}